package cn.tedu.hotel.serve.controller;

import cn.tedu.hotel.serve.pojo.dto.HotelCurrentRoomsDTO;
import cn.tedu.hotel.serve.pojo.dto.OrdersDTO;
import cn.tedu.hotel.serve.pojo.entity.Orders;
import cn.tedu.hotel.serve.pojo.vo.*;
import cn.tedu.hotel.serve.security.UserLoginPrincipal;
import cn.tedu.hotel.serve.service.IOrdersService;
import cn.tedu.hotel.serve.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/orders")
@Api(tags = "05. 订单管理模块")
@Validated
public class OrdersController {

    @Autowired
    IOrdersService ordersService;

    @PostMapping("/user/addOrder")
    @ApiOperation("前台添加订单")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addOrder(@Validated OrdersDTO ordersDTO) {
        log.debug("开始【处理前台增加订单】的请求");
        ordersService.addOrder(ordersDTO);
        return JsonResult.ok();
    }

    @GetMapping("/user/{id:[0-9]+}/updateOrderStateByOrderId")
    @ApiOperation("前台根据订单ID修改支付状态")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateOrderStateByOrderId(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值")Long id
    ){
        log.debug("开始处理【前台根据订单ID：{}，修改支付状态】的请求",id);
        ordersService.updateOrderStateByOrderId(id);
        return JsonResult.ok();
    }

    @PostMapping("/user/getCurrentRoomsByThreeColumn")
    @ApiOperation("前台查询当前酒店房间信息")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<HotelCurrentRoomsVO>> getCurrentRoomsByThreeColumn(
            HotelCurrentRoomsDTO hotelCurrentRoomsDTO) {
        log.debug("开始处理【前台查询当前酒店房间信息】的请求");
        List<HotelCurrentRoomsVO> currentRoomsByThreeColumn =
                ordersService.getCurrentRoomsByThreeColumn(hotelCurrentRoomsDTO);
        return JsonResult.ok(currentRoomsByThreeColumn);
    }

    @GetMapping("/user/getOrdersList")
    @ApiOperation("前台查询该用户的订单集合")
    @ApiOperationSupport(order = 401)
    public JsonResult<List<UserOrdersListVO>> getUserOrdersList(
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal) {
        log.debug("开始处理【前台查询该用户的订单集合】的请求");
        List<UserOrdersListVO> userOrdersList =
                ordersService.getUserOrdersList(userLoginPrincipal.getId());
        return JsonResult.ok(userOrdersList);
    }

    @GetMapping("/user/getLatestOrderById")
    @ApiOperation("前台根据用户ID获取最新订单信息")
    @ApiOperationSupport(order = 402)
    public JsonResult<OrderVO> getLatestOrderById(
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal
    ){
        log.debug("开始处理【前台根据用户ID：{}获取最新订单信息】的请求",userLoginPrincipal.getId());
        OrderVO orderVO = ordersService.getLatestOrderById(userLoginPrincipal.getId());
        return JsonResult.ok(orderVO);
    }

    @GetMapping("/user/{orderId:[0-9]+}/cancelOrderOrDrawback/{function}")
    @ApiOperation("根据订单ID和功能取消订单或者退款")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> cancelOrderOrDrawback(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值")Long orderId,
            @PathVariable String function
    ){
        log.debug("开始处理【根据订单ID：{}和功能：{}取消订单或者退款】的请求",orderId,function);
        ordersService.cancelOrderOrDrawback(orderId,function);
        return JsonResult.ok();
    }

    @PostMapping("/admin/{id:[0-9]+}/deleteAdminOrdersById")
    @ApiOperation("后台根据订单ID删除订单")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 600)
    public JsonResult<Void> deleteAdminOrdersById(
            @PathVariable @Range(min = 1,message = "请提交有效值") Long id
    ){
        log.debug("开始处理【后台根据订单ID:{}删除订单】的请求",id);
        ordersService.deleteAdminOrdersById(id);
        return JsonResult.ok();
    }

    @GetMapping("/admin/getAdminUserOrdersList")
    @ApiOperation("后台查询订单列表")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<OrdersWithStateListVO>> getAdminUserOrdersList() {
        log.debug("开始处理【后台查询订单列表】的请求");
        List<OrdersWithStateListVO> list = ordersService.getAdminUserOrdersList();
        return JsonResult.ok(list);
    }

}
